<?php
class ArrivalTime extends DB
{
    var $_table = 'arrival_time';

    function __construct()
    {
	parent::__construct();

	$this->_field = array('id'=>'ID','manager_name'=>'Менеджер','date_time'=>'Время','ip'=>'IP','reason'=>'Примечание','cost'=>'Штраф');

	$this->jqGridOptions = array_merge
	(
	    array
	    (
		'url'		=> 'timeReport.php',
		'datatype'	=> 'json',
		'viewrecords'	=> true,
		'colNames'	=> array_values($this->_field),
		'sortname'	=> 'date_time',
		'sortorder'	=> 'desc',
		'pager'		=> 'jqg-TimeReport-pager',
		'colModel'	=> array
		(
		    array('name'=>'id','hidden'=>true,'width'=>50),
		    array('name'=>'manager_name','width'=>120,'sortable'=>false),
		    array('name'=>'date_time','width'=>150),
		    array('name'=>'ip','width'=>100,'sortable'=>false),
		    array('name'=>'reason','width'=>200,'sortable'=>false),
		    array('name'=>'cost','width'=>150,'sortable'=>false,'editable'=>true),
		),
		'cellEdit'	=> true,
		'cellurl'	=> 'timeReport.php',
		'cellsubmit'	=> 'remote',

		'altRows'	=> true,
		'footerrow'	=> true,
		'userDataOnFooter'	=> true
            )
	);
    }

    function setManager($id)
    {
	if ( is_numeric($id) )
	{
	    $this->_manager_id = $id;
	    return $this;
	}
    }

    function countAll()
    {
	$this->_query = sprintf('SELECT `id` FROM `%s` %s GROUP BY DATE(`date_time`),`manager_id`', $this->_table, $this->_where($this->_filter,'AND',false));

	if ( $this->_result = mysql_query($this->_query) )
	{
	    return mysql_affected_rows();
	}
    }

    function timeReport($start,$stop)
    {
	$this->_filter[] = array('`type`','login');

	if ( !empty($start) && !empty($stop) )
	{
	    $this->_filter[] = sprintf('`date_time` BETWEEN \'%s\' AND \'%s 23:59:59\'',$start,$stop);
	}

	$this->_query = sprintf
	(
	    'SELECT MIN(`id`) AS `min_id`,'
		.'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`%1$s`.`manager_id`),'
		.'DATE_FORMAT(MIN(`date_time`),\'%%d.%%m.%%Y, %%h:%%i\'),'
		.'`ip`,'
		.'`reason`,'
//		.'(SELECT `reason` FROM `%1$s` WHERE `id` LIMIT 1) AS `reason`,'
		.'`cost_fine`'
	    .' FROM `%1$s`'
	    .' %2$s GROUP BY DATE(`date_time`),`manager_id` %3$s %4$s',
	    $this->_table,
	    $this->_where($this->_filter,'AND',false),
//	    $this->_order('manager_id','ASC'),
	    $this->_order($this->_order_field,$this->_order_dir),
	    $this->_limit()
	);

	if ( $this->_result = mysql_query($this->_query) )
	{
	    while ( $row = mysql_fetch_row($this->_result) )
	    {
		$rows[] = array('id'=>$row[0],'cell'=>$row);
	    }

	    $data['rows'] = $rows;
	}
	else { die($this->_query); }

	$data['records']= $this->countAll();
	$data['page']	= $this->curPage();
	$data['total']	= ceil($data['records']/$this->_limit_count);

	$this->_query = sprintf('SELECT SUM(`cost_fine`) FROM %1$s %2$s',$this->_table,$this->_where($this->_filter,'AND',false));

	if ( $this->_result = mysql_query($this->_query) )
	    if ( $row = mysql_fetch_row($this->_result) )
		$data['userdata']['cost'] = $row[0];

	return $data;
    }
}
?>